package alo.p5;
//基于链表实现队列
public class LLQueue {
    private ListNode frontNode;
    private ListNode rearNode;
    private LLQueue(){
        this.frontNode=null;
        this.rearNode=null;
    }
    public static LLQueue createQueue(){
        return new LLQueue();
    }
    public boolean isEmpty(){
        return frontNode==null;
    }
    public void enQueue(int data){
        ListNode newNode=new ListNode(data);
        if (rearNode!=null){
            rearNode.setNext(newNode);
        }
        rearNode=newNode;
        if (frontNode==null){
            frontNode=rearNode;
        }
    }
    public int deQueue() throws EmptyQueueException {
        int data= Integer.parseInt(null);
        if (isEmpty()){
            throw new EmptyQueueException("Queue Empty");
        }else {
            data= frontNode.getData();
            frontNode=frontNode.getNext();
        }
        return data;
    }

    private class EmptyQueueException extends Throwable {
        public EmptyQueueException(String queue_empty) {
            System.out.println(queue_empty);
        }

        @Override
        public void printStackTrace() {
            System.out.println("Queue Empty");
        }
    }
    //链表结点
    class ListNode {
        private int data;
        private ListNode next;

        public ListNode(Integer data) {
            this.data = data;
        }
        public int getData() {
            return data;
        }
        public void setData(int data) {
            this.data = data;
        }
        public ListNode getNext() {
            return next;
        }
        public void setNext(ListNode next) {
            this.next = next;
        }


    }
}
